package com.thinkgem.jeesite.modules.gen.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thinkgem.jeesite.common.persistence.BaseConstants;
import com.thinkgem.jeesite.modules.gen.dao.GenProjectMapper;
import com.thinkgem.jeesite.modules.gen.entity.GenProject;
import com.thinkgem.jeesite.modules.gen.entity.GenProjectModule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 项目配置Service
 *
 * @author kongxiangsheng
 * @version 2018-11-05
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class GenProjectService extends ServiceImpl<GenProjectMapper, GenProject> implements IService<GenProject> {
    @Autowired
    private GenProjectModuleService genProjectModuleService;

    /**
     * 根据id获取（含子表）
     *
     * @param id
     * @return
     */
    public GenProject getWithChildren(String id) {
        GenProject genProject = this.getById(id);
        genProject.setGenProjectModuleList(genProjectModuleService.getByGenProjectId(id));
        return genProject;
    }

    /**
     * 保存数据（含子表）
     *
     * @param genProject
     */
    @Transactional( rollbackFor = Exception.class)
    public void saveWithChildren(GenProject genProject) {
        this.saveOrUpdate(genProject);

        for (GenProjectModule genProjectModule : genProject.getGenProjectModuleList()) {
            if (genProjectModule.getId() == null) {
                continue;
            }
            if (BaseConstants.DEL_FLAG_NORMAL.equals(genProjectModule.getDelFlag())) {
                // TODO: 有没有办法不用加下面这行
                genProjectModule.setProjectId(genProject.getId());
                genProjectModuleService.saveOrUpdate(genProjectModule);
            } else {
                genProjectModuleService.removeById(genProjectModule.getId());
            }
        }
    }

    /**
     * 删除数据（含子表数据）
     *
     * @param genProject
     */
    @Transactional( rollbackFor = Exception.class)
    public void deleteWithChildren(GenProject genProject) {
        this.removeById(genProject.getId());
        genProjectModuleService.deleteByGenProjectId(genProject.getId());
    }

    /**
     * 查询所有项目（用于字典选择）
     *
     * @return
     */
    public List<GenProject> getAllProjectDict() {
        QueryWrapper queryWrapper = new QueryWrapper<GenProject>();
        queryWrapper.select(GenProject.COL_ID, GenProject.COL_CODE, GenProject.COL_NAME);
        return this.list(queryWrapper);
    }

    /**
     * 获取项目的基础路径
     * @param id
     * @return
     */
    public String getProjectPackage(String id) {
        GenProject genProject = this.getById(id);
        if(null == genProject){
            return "";
        }
        return genProject.getBasePackage();
    }
}